package com.zhy.generator.codegen.mybatis.javacontroller.elements;
import com.zhy.generator.codegen.api.IntrospectedColumn;
import com.zhy.generator.codegen.dom.java.FullyQualifiedJavaType;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.dom.java.Method;
import com.zhy.generator.codegen.dom.java.TopLevelClass;
import com.zhy.generator.codegen.mybatis.javacontroller.JavaControllerMethodConstant;
import com.zhy.generator.codegen.mybatis.javahelper.JavaHelperMethodConstant;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 源
 */
public class ListMethodGenerator extends AbstractJavaControllerMethodGenerator {
    @Override
    public void addControllerElements(TopLevelClass tlc) {

        Method listMethod = new Method(JavaControllerMethodConstant.LIST_METHOD);
        listMethod.setVisibility(JavaVisibility.PUBLIC);

        String voType = introspectedTable.getVoType();
        FullyQualifiedJavaType javaVoType = new FullyQualifiedJavaType(voType);

        FullyQualifiedJavaType returnType = getReturnType();
        FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance();

        listType.addTypeArgument(javaVoType);
        returnType.addTypeArgument(listType);

        tlc.addImportedType(returnType);
        tlc.addImportedType(listType);
        tlc.addImportedType(javaVoType);

        listMethod.setReturnType(returnType);

        addParameter(tlc, listMethod);

        addMethodAnnotations(tlc, listMethod);

        tlc.addMethod(listMethod);

        listMethod.addBodyLine("//组装参数");
        listMethod.addBodyLine("Map<String, String> params = Maps.newHashMap();");

        listMethod.addBodyLine("//查询总数");
        listMethod.addBodyLine("Long allCount = " + getServiceShortName() + ".getAllCount(params);");

        listMethod.addBodyLine("//排序");
        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        if(!CollectionUtils.isEmpty(primaryKeyColumns)){
            StringBuffer sb = new StringBuffer();
            primaryKeyColumns.forEach(key ->{
                if(StringUtils.isEmpty(sb.toString())){
                    sb.append(key.getActualColumnName());
                }else{
                    sb.append(",").append(key.getActualColumnName());
                }
            });
            listMethod.addBodyLine("params.put(\"orderby\", \""+sb.toString()+" desc\");");
        }

        listMethod.addBodyLine("//分页");
        listMethod.addBodyLine("int p = param.getPageIndex();");
        listMethod.addBodyLine("int prow = param.getPageSize();");
        listMethod.addBodyLine("int start = (p - 1) * prow;");
        listMethod.addBodyLine("params.put(\"limit\", start + \",\" + prow);");

        FullyQualifiedJavaType javaPOType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        listMethod.addBodyLine("List<" + javaPOType.getShortName() + "> all = " + getServiceShortName() + ".getAll(params);");

        tlc.addImportedType(javaPOType);

        listMethod.addBodyLine("if (CollectionUtils.isEmpty(all)) {");
        listMethod.addBodyLine("   return ResultUtil.jsonMsg(ResultErrCodeEnum.SUCCESS.getStatus(), \"未设置任何数据\", null);");
        listMethod.addBodyLine("}");

        FullyQualifiedJavaType helperType = new FullyQualifiedJavaType(introspectedTable.getHelperType());
        tlc.addImportedType(helperType);
        listMethod.addBodyLine("List<" + javaVoType.getShortName() + "> result = "+helperType.getShortName()+"."+ JavaHelperMethodConstant.PO_TO_VO_BATCH +"(all);");

        tlc.addImportedType("com.google.common.collect.Lists");
        tlc.addImportedType("com.google.common.collect.Maps");
        tlc.addImportedType("java.util.Map");
        tlc.addImportedType("org.springframework.util.CollectionUtils");

        listMethod.addBodyLine("return ResultUtil.jsonPagerMsg(ResultErrCodeEnum.SUCCESS.getStatus(), \"操作成功\", result, allCount, p, prow);");
    }

    @Override
    public String getMethodStr() {
        return JavaControllerMethodConstant.LIST_METHOD;
    }


}
